package pl.krzosekm.vehicleregister.gui;
import java.awt.Component;
import java.awt.Dimension;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;

import org.jdesktop.application.Action;
import org.jdesktop.application.Application;
import org.jdesktop.application.ApplicationActionMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import pl.krzosekm.vehicleregister.model.Customer;
import pl.krzosekm.vehicleregister.service.CustomerService;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
@SuppressWarnings("serial")
public class CustomerPanel extends javax.swing.JPanel {
	
	private String[] locations = {"classpath:pl/krzosekm/vehicleregister/resources/applicationContext-service.xml"};
	private ApplicationContext ctx = new FileSystemXmlApplicationContext(locations);
		
	@Autowired
	private CustomerService customerService;
	private JButton repairRegisterButton;
	private JTextField nameTF;
	private JButton newCustomerButton;
	private JButton saveCustButton;
	private JTextField phoneNumberTF;
	private JLabel phoneNumberLabel;
	private JTextField surnameTF;
	private JLabel surnameLabel;
	private JLabel nameLabel;
	private JTextField idNumberTF;
	private JLabel idNumberLabel;
	private JButton jButton;

	/**
	* Auto-generated main method to display this 
	* JPanel inside a new JFrame.
	*/
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.getContentPane().add(new CustomerPanel(null));
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}
	
	public CustomerPanel(JButton jButton) {
		super();
		this.jButton = jButton;
		customerService = (CustomerService)ctx.getBean("customerService");
		initGUI();
	}
	
	private void initGUI() {
	    try {
		GroupLayout thisLayout = new GroupLayout((JComponent)this);
	        this.setLayout(thisLayout);
		setPreferredSize(new Dimension(400, 300));
		{
		    idNumberLabel = new JLabel();
		    idNumberLabel.setName("idNumberLabel");
		}
		{
			repairRegisterButton = jButton;
			repairRegisterButton.setName("repairRegisterButton");
		}
		{
		    idNumberTF = new JTextField();
		}
		{
		    nameLabel = new JLabel();
		    nameLabel.setName("nameLabel");
		}
		{
		    nameTF = new JTextField();
		}
		{
		    surnameLabel = new JLabel();
		    surnameLabel.setName("surnameLabel");
		}
		{
		    surnameTF = new JTextField();
		}
		{
		    phoneNumberLabel = new JLabel();
		    phoneNumberLabel.setName("phoneNumberLabel");
		}
		{
		    phoneNumberTF = new JTextField();
		}
		{
		    saveCustButton = new JButton();
		    saveCustButton.setName("saveCustomerButton");
		    saveCustButton.setAction(getAppActionMap().get("saveCustomerAction"));
		}
		{
		    newCustomerButton = new JButton();
		    newCustomerButton.setName("newCustomerButton");
		    newCustomerButton.setAction(getAppActionMap().get("newCustomerAction"));
		}
		    thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
		    	.addContainerGap()
		    	.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
		    	    .addComponent(idNumberTF, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
		    	    .addComponent(idNumberLabel, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
		    	.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
		    	.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
		    	    .addComponent(nameTF, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
		    	    .addComponent(nameLabel, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
		    	.addGap(18)
		    	.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
		    	    .addComponent(surnameTF, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
		    	    .addComponent(surnameLabel, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
		    	.addGap(16)
		    	.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
		    	    .addComponent(phoneNumberTF, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
		    	    .addComponent(phoneNumberLabel, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))
		    	.addGap(112)
		    	.addGroup(thisLayout.createParallelGroup()
		    	    .addGroup(thisLayout.createSequentialGroup()
		    	        .addComponent(repairRegisterButton, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE)
		    	        .addGap(0, 0, Short.MAX_VALUE))
		    	    .addGroup(thisLayout.createSequentialGroup()
		    	        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
		    	        .addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
		    	            .addComponent(saveCustButton, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE)
		    	            .addComponent(newCustomerButton, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE, GroupLayout.PREFERRED_SIZE))))
		    	.addContainerGap(15, 15));
		    thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
		    	.addContainerGap()
		    	.addGroup(thisLayout.createParallelGroup()
		    	    .addGroup(thisLayout.createSequentialGroup()
		    	        .addGroup(thisLayout.createParallelGroup()
		    	            .addComponent(phoneNumberLabel, GroupLayout.Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 104, GroupLayout.PREFERRED_SIZE)
		    	            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
		    	                .addComponent(surnameLabel, GroupLayout.PREFERRED_SIZE, 92, GroupLayout.PREFERRED_SIZE)
		    	                .addGap(12))
		    	            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
		    	                .addComponent(nameLabel, GroupLayout.PREFERRED_SIZE, 92, GroupLayout.PREFERRED_SIZE)
		    	                .addGap(12))
		    	            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
		    	                .addComponent(idNumberLabel, GroupLayout.PREFERRED_SIZE, 92, GroupLayout.PREFERRED_SIZE)
		    	                .addGap(12)))
		    	        .addGroup(thisLayout.createParallelGroup()
		    	            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
		    	                .addComponent(phoneNumberTF, GroupLayout.PREFERRED_SIZE, 118, GroupLayout.PREFERRED_SIZE)
		    	                .addGap(0, 107, Short.MAX_VALUE))
		    	            .addGroup(thisLayout.createSequentialGroup()
		    	                .addComponent(surnameTF, GroupLayout.PREFERRED_SIZE, 225, GroupLayout.PREFERRED_SIZE)
		    	                .addGap(0, 0, Short.MAX_VALUE))
		    	            .addGroup(thisLayout.createSequentialGroup()
		    	                .addComponent(nameTF, GroupLayout.PREFERRED_SIZE, 225, GroupLayout.PREFERRED_SIZE)
		    	                .addGap(0, 0, Short.MAX_VALUE))
		    	            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
		    	                .addComponent(idNumberTF, GroupLayout.PREFERRED_SIZE, 118, GroupLayout.PREFERRED_SIZE)
		    	                .addGap(0, 107, Short.MAX_VALUE)))
		    	        .addGap(48))
		    	    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
		    	        .addGap(0, 0, Short.MAX_VALUE)
		    	        .addComponent(repairRegisterButton, GroupLayout.PREFERRED_SIZE, 190, GroupLayout.PREFERRED_SIZE)
		    	        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
		    	        .addComponent(newCustomerButton, GroupLayout.PREFERRED_SIZE, 61, GroupLayout.PREFERRED_SIZE)
		    	        .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
		    	        .addComponent(saveCustButton, GroupLayout.PREFERRED_SIZE, 61, GroupLayout.PREFERRED_SIZE)))
		    	.addContainerGap());
		    Application.getInstance().getContext().getResourceMap(getClass()).injectComponents(this);
	    } catch (Exception e) {
		e.printStackTrace();
	    }
	}
	
	
	public CustomerService getCustomerService() {
		return customerService;
	}

	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}
	
	
	private ApplicationActionMap getAppActionMap() {
		return Application.getInstance().getContext().getActionMap(this);
	}

	
	@Action
	public void saveCustomerAction() 
	{
	    Long id = Long.valueOf(idNumberTF.getText());
	    String name = nameTF.getText();
	    String surname = surnameTF.getText();
	    String phoneNumber = phoneNumberTF.getText();
		
	    Customer customer = new Customer(id, name, surname, phoneNumber);
	    customerService.save(customer);
	    idNumberTF.setText(null);
	    nameTF.setText(null);
	    phoneNumberTF.setText(null);
	    surnameTF.setText(null);
	    saveCustButton.setText("Zmień");
	    saveCustButton.setAction(getAppActionMap().get("editCustomerAction"));
	}
	
	@Action
	public void editCustomerAction() {
	    saveCustButton.setText("Zapisz");
	    saveCustButton.setAction(getAppActionMap().get("saveCustomerAction"));
	}
	
	@Action
	public void newCustomerAction() {
	    idNumberTF.setText(null);
	    nameTF.setText(null);
	    phoneNumberTF.setText(null);
	    surnameTF.setText(null);
	}



}
